package com.sugar.psych.assessment.module.business.dal.mysql.userpointsrecord;

import java.util.*;

import com.sugar.psych.assessment.framework.common.pojo.PageResult;
import com.sugar.psych.assessment.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.sugar.psych.assessment.framework.mybatis.core.mapper.BaseMapperX;
import com.sugar.psych.assessment.framework.mybatis.core.query.MPJLambdaWrapperX;
import com.sugar.psych.assessment.module.business.controller.admin.feedback.vo.FeedbackRespVO;
import com.sugar.psych.assessment.module.business.controller.app.userpointsrecord.vo.AppUserPointsRecordRespVO;
import com.sugar.psych.assessment.module.business.dal.dataobject.feedback.FeedbackDO;
import com.sugar.psych.assessment.module.business.dal.dataobject.user.UserDO;
import com.sugar.psych.assessment.module.business.dal.dataobject.userpointsrecord.UserPointsRecordDO;
import org.apache.ibatis.annotations.Mapper;
import com.sugar.psych.assessment.module.business.controller.admin.userpointsrecord.vo.*;
import org.apache.ibatis.annotations.Select;

/**
 * 用户积分记录 Mapper
 *
 * @author Ysugar
 */
@Mapper
public interface UserPointsRecordMapper extends BaseMapperX<UserPointsRecordDO> {

    default PageResult<UserPointsRecordRespVO> selectPage(UserPointsRecordPageReqVO reqVO) {
        return selectJoinPage(reqVO, UserPointsRecordRespVO.class, new MPJLambdaWrapperX<UserPointsRecordDO>()
                .selectAll(UserPointsRecordDO.class)
                .selectAs(UserDO::getNickname, UserPointsRecordRespVO::getNickname)
                .selectAs(UserDO::getMobile, UserPointsRecordRespVO::getMobile)
                .leftJoin(UserDO.class, UserDO::getId, UserPointsRecordDO::getUserId)
                .eqIfPresent(UserPointsRecordDO::getUserId, reqVO.getUserId())
                .eqIfPresent(UserPointsRecordDO::getTitle, reqVO.getTitle())
                .eqIfPresent(UserPointsRecordDO::getType, reqVO.getType())
                .eqIfPresent(UserPointsRecordDO::getObtainedPoints, reqVO.getObtainedPoints())
                .betweenIfPresent(UserPointsRecordDO::getRecordTime, reqVO.getRecordTime())
                .eqIfPresent(UserPointsRecordDO::getTotalPoints, reqVO.getTotalPoints())
                .betweenIfPresent(UserPointsRecordDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(UserPointsRecordDO::getId));
    }

    @Select("select bupr.*, date_format(bupr.record_time, '%Y年%m月') as month_item " +
            "from business_user_points_record bupr " +
            "where bupr.user_id = #{param1} " +
            "order by bupr.record_time desc")
    List<AppUserPointsRecordRespVO.PointRecord> appSelectList(Long loginUserId);
}